﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml;
using System.Globalization;

namespace SPVisualDev.VSAddin.Dialogs.PropertySheets
{
    internal class PropSheetField : PropSheetBase
    {
        public PropSheetField(string DefaultResourceFile, EnvDTE.ProjectItem ParentFeatureFolder, ElementInfo Element)
        {
            this.DefaultResourceFile = DefaultResourceFile;
            this.ParentFeatureFolder = ParentFeatureFolder;

            //this.Name = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.DisplayName = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.Group = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);

            this.ElementInfo = Element;
            this.ElementFileName = Element.ElementFileName;

            if (Element.XmlElementNode != null)
                this.PropXmlNode = Element.XmlElementNode;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("ID"), ReadOnly(false), RefreshProperties(RefreshProperties.None)]
        public string ID
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Name"), RefreshProperties(RefreshProperties.None), ReadOnly(false)]
        public string Name
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Display name"), RefreshProperties(RefreshProperties.None), ReadOnly(false), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer DisplayName
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Field type"), RefreshProperties(RefreshProperties.None), ReadOnly(false), TypeConverter(typeof(FieldTypeExpandableConverter))]
        public string Type
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Static name"), RefreshProperties(RefreshProperties.None), ReadOnly(false)]
        public string StaticName
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Group"), RefreshProperties(RefreshProperties.None), ReadOnly(false), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Group
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Read only"), RefreshProperties(RefreshProperties.None), ReadOnly(false)]
        public bool? ReadOnly
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Read only"), RefreshProperties(RefreshProperties.None), ReadOnly(false)]
        public bool? Hidden
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Sealed"), RefreshProperties(RefreshProperties.None), ReadOnly(false)]
        public bool? Sealed
        {
            get;
            set;
        }

        public override string ToString()
        {
            return "<Field> " + this.Name;
        }

        public class FieldTypeExpandableConverter : ExpandableObjectConverter
        {
            public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
            {
                return false;
            }

            public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
            {
                return true;
            }

            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            {
                return true;
            }

            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                return true;
            }

            public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
            {
                List<string> list = new List<string>();

                foreach (string s in Enum.GetNames(typeof(FieldTypeEnum)))
                {
                    if (s != "Invalid")
                        list.Add(s);
                }

                return new StandardValuesCollection(list);

            }

            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                return value;
            }

            public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            {
                return false;
            }
        }

    }

}
